package cn.itcast.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.entity.Result;
import cn.itcast.service.CountViewService;
import cn.itcast.vo.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;




@RestController
@RequestMapping("/report")
public class CountViewController {

    @Reference
    private CountViewService countViewService;
    /**
     * 查询会员统计数
     *
     * @return
     */
    @GetMapping("/member/count")
    public Result VipCount() {

        try {

            List<CountVo> list = countViewService.vipCount();
            List<String> months = new ArrayList<>();
            List<Integer> memberCount = new ArrayList<>();
            for (CountVo vipCountVo : list) {
                months.add(vipCountVo.getValue());
                memberCount.add(vipCountVo.getCount());
            }
            DataVo dataVo = getmonths(months, memberCount);

            return new Result(true,"查询成功",dataVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "查询失败");
    }

    /**套餐统计
     * setmealNames
     * setmealCount
     * @return
     */
    @GetMapping("/setmeals")
    public Result setmealCount() {

        try {
            List<CountVo> list = countViewService.setmealCount();

            List<Date2>  dates= new ArrayList<>();

            for (CountVo vipCountVo : list) {
                Date2 date = new Date2();
                date.setName(vipCountVo.getValue());
                date.setValue(vipCountVo.getCount());
                dates.add(date);
            }



            return new Result(true,"查询成功",dates);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "查询失败");
    }

    /**收入统计
     * setmealNames
     * setmealCount
     * @return
     */
    @GetMapping("/income")
    public Result incomeCount() {

        try {
            List<CountVo> list = countViewService.incomeCount();
            List<String> months = new ArrayList<>();
            List<Integer> memberCount = new ArrayList<>();
            for (CountVo vipCountVo : list) {
                months.add(vipCountVo.getValue());
                memberCount.add(vipCountVo.getCount());
            }

            DataVo dataVo = getmonths(months, memberCount);


            return new Result(true,"查询成功",dataVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "查询失败");
    }

    @GetMapping("/order")
    public Result orderCount() {

        try {
            List<String> months = new ArrayList<>();
            List<Integer> count = new ArrayList<>();
            List<Long> price = new ArrayList<>();
           List<SumCount> list= countViewService.orderCount();
            for (SumCount sumCount : list) {
                months.add(sumCount.getMonths());
                count.add(sumCount.getCount());
                price.add(sumCount.getPrice());
            }


            DataVo dataVo = getmonths(months, count, price);


            return new Result(true,"查询成功",dataVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "查询失败");
    }

    @GetMapping("/business")
    public Result business(){

        try {

            Data_Plus dataPlus = countViewService.findBusiness();
            return new Result(true,"查询成功",dataPlus);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"查询失败");
    }

    @RequestMapping("/export/business")
    public Result export(HttpServletResponse response) {
        XSSFWorkbook workbook = null;
        try {
            response.setHeader("Content-disposition", "attachment;filename=report.xlsx");
            ServletOutputStream outputStream = response.getOutputStream();
            //获得模板文件路径
            String path = this.getClass().getClassLoader().getResource("static/template/report_template.xlsx").getPath();
            //创建WorkBook对象，读取模板对象.
            workbook = new XSSFWorkbook(path);
            //获得sheet工作对象
            XSSFSheet sheet = workbook.getSheet("Sheet1");
            //获得数据
            Data_Plus dataPlus = countViewService.findBusiness();
            //写数据
            //日期
            XSSFRow row = sheet.getRow(2);
            XSSFCell cell = row.getCell(5);
            cell.setCellValue(dataPlus.getReportDate());
            //新增会员
            XSSFRow row1 = sheet.getRow(4);
            XSSFCell cell1 = row1.getCell(5);
            cell1.setCellValue(dataPlus.getTodayNewMember());
            //总会员数
            XSSFRow row2 = sheet.getRow(4);
            XSSFCell cell2 = row2.getCell(7);
            cell2.setCellValue(dataPlus.getTotalMember());
            //本周新增会员数
            XSSFRow row3 = sheet.getRow(5);
            XSSFCell cell3 = row3.getCell(5);
            cell3.setCellValue(dataPlus.getThisWeekNewMember());
            //本月新增会员数
            XSSFRow row4 = sheet.getRow(5);
            XSSFCell cell4 = row4.getCell(7);
            cell4.setCellValue(dataPlus.getThisMonthNewMember());
            //今日预约数
            XSSFRow row5 = sheet.getRow(7);
            XSSFCell cell5 = row5.getCell(5);
            cell5.setCellValue(dataPlus.getTodayOrderNumber());
            //今日到诊数
            XSSFRow row6 = sheet.getRow(7);
            XSSFCell cell6 = row6.getCell(7);
            cell6.setCellValue(dataPlus.getTodayVisitsNumber());
            //本周预约数
            XSSFRow row7 = sheet.getRow(8);
            XSSFCell cell7 = row7.getCell(5);
            cell7.setCellValue(dataPlus.getThisWeekOrderNumber());
            //本周到诊数
            XSSFRow row8 = sheet.getRow(8);
            XSSFCell cell8 = row8.getCell(7);
            cell8.setCellValue(dataPlus.getThisWeekVisitsNumber());
            //本月预约数
            XSSFRow row9 = sheet.getRow(9);
            XSSFCell cell9 = row9.getCell(5);
            cell9.setCellValue(dataPlus.getThisMonthOrderNumber());
            //本月到诊数
            XSSFRow row10 = sheet.getRow(9);
            XSSFCell cell10 = row10.getCell(7);
            cell10.setCellValue(dataPlus.getThisMonthVisitsNumber());
            //套餐
            List<HotSetmeal> hotSetmeal = dataPlus.getHotSetmeal();
            int index = 12;
            for (int i = 0; i < hotSetmeal.size(); i++) {
                HotSetmeal setmeal = hotSetmeal.get(i);
                //套餐名称
                XSSFRow row11 = sheet.getRow(index);
                if (ObjectUtil.isNull(row11)) {
                    XSSFRow row666 = sheet.createRow(index);
                    XSSFCell cell661 = row666.createCell(4);
                    cell661.setCellValue(setmeal.getName() + "");
                    XSSFCell cell662 = row666.createCell(5);
                    cell662.setCellValue(setmeal.getSetmeal_count());
                    XSSFCell cell663 = row666.createCell(6);
                    cell663.setCellValue(setmeal.getProportion());
                    XSSFCell cell664 = row666.createCell(7);
                    cell664.setCellValue(setmeal.getText() + "");
                    index++;

                } else {
                    XSSFCell cell11 = row11.getCell(4);
                    cell11.setCellValue(setmeal.getName() + "");


                    //预约数量
                    XSSFRow row12 = sheet.getRow(index);
                    XSSFCell cell12 = row12.getCell(5);
                    cell12.setCellValue(setmeal.getSetmeal_count());
                    //占比
                    XSSFRow row13 = sheet.getRow(index);
                    XSSFCell cell13 = row13.getCell(6);
                    cell13.setCellValue(setmeal.getProportion());
                    //备注
                    XSSFRow row14 = sheet.getRow(index);
                    XSSFCell cell14 = row14.getCell(7);
                    cell14.setCellValue(setmeal.getText() + "");
                    index++;
                }
            }
            //响应客户端
            workbook.write(outputStream);
            return new Result(true,"下载成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                try {
                    if (workbook != null) {
                    workbook.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

        return new Result(false,"下载失败");
    }

    public DataVo getmonths(List<String> list,List<Integer> count){
        LocalDateTime dateTime = LocalDateTime.now();
        DataVo dataVo = new DataVo();
        List<Integer> count1=new ArrayList<>();
        for (int i = 0; i <12; i++) {
            count1.add(0);
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        List<String> months=new ArrayList<>();
        for (int i = 0; i <12; i++) {
            LocalDateTime plusMonths = dateTime.plusMonths(-11+i);
            String month = plusMonths.format(formatter);
            months.add(month);
        }
        for (int i = 0; i < list.size(); i++) {
            String s1 = list.get(i);
            for (int i1 = 0; i1 < months.size(); i1++) {
                String s2 = months.get(i1);
                if(StringUtils.equals(s1,s2)){
                    int index = months.indexOf(s2);
                    count1.set(index,count.get(i));
                }
            }

        }
        dataVo.setMonths(months);
        dataVo.setCount(count1);

        return dataVo;
    }

    public DataVo getmonths(List<String> list,List<Integer> count,List<Long> price){
        LocalDateTime dateTime = LocalDateTime.now();
        DataVo dataVo = new DataVo();
        List<Integer> count1=new ArrayList<>();
        for (int i = 0; i <12; i++) {
            count1.add(0);
        }
        List<Long> price1=new ArrayList<>();
        for (int i = 0; i <12; i++) {
            price1.add(0L);
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        List<String> months=new ArrayList<>();
        for (int i = 0; i <12; i++) {
            LocalDateTime plusMonths = dateTime.plusMonths(-11+i);
            String month = plusMonths.format(formatter);
            months.add(month);
        }
        for (int i = 0; i < list.size(); i++) {
            String s1 = list.get(i);
            for (int i1 = 0; i1 < months.size(); i1++) {
                String s2 = months.get(i1);
                if(StringUtils.equals(s1,s2)){
                    int index = months.indexOf(s2);
                    count1.set(index,count.get(i));
                    price1.set(index,price.get(i));
                }
            }

        }
        dataVo.setMonths(months);
        dataVo.setCount(count1);
        dataVo.setPrice(price1);
        return dataVo;
    }


}
